#!/usr/bin/python
# -*- coding: utf-8 -*-

import os, zipfile, sys;

path = os.getcwd();


if sys.argv[1] == "clean":
    os.remove(path+'/tanz.zip');
else:
    print "Package application dans le répertoire: "+path;

    print "Mise a jour de la version...";
    descriptorSrc = open(path+'/descriptor.xml', 'r');
    descriptorDest = open(path+'/descriptorTmp.xml', 'w');

    for line in descriptorSrc:
        if line.find("<version>") != -1:
            descriptorDest.write("<version>"+sys.argv[1]+"</version>\n");
        else:
            descriptorDest.write(line);

    descriptorSrc.close();
    descriptorDest.close();

    os.remove(path+'/descriptor.xml');
    os.rename(path+'/descriptorTmp.xml', path+'/descriptor.xml');


    print "Création de l'archive...";

    file = zipfile.ZipFile("tanz.zip", "w");

    for name in os.listdir(path):
        if name != "package.py" <> name != "tanz.zip":
            file.write(name, name, zipfile.ZIP_DEFLATED);


    file.close();

    print "Archive crée !";
